package com.xinghou.mq.impl;

import com.spatial4j.core.context.SpatialContext;
import com.xinghou.mq.ShareCardInterface;
import com.xinghou.platform.dao.bases.util.StringUtils;
import com.xinghou.platform.dao.main.*;
import com.xinghou.platform.lucene.main.SharedCardLuceneService;
import com.xinghou.platform.model.main.SharedCard;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.server.api.log.LoggerFactory;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Closeable;
import java.io.IOException;

@Service("mqCardService")
public class CardInterfaceImpl implements ShareCardInterface, Closeable {
    private static Logger logger= LoggerFactory.getServerInfoLogger(CardInterfaceImpl.class);

    @Resource
    private UserDAO userDAO;
    @Resource
    private UserGeoDAO userGeoDAO;
    @Resource
    private CityDAO cityDAO;
    @Resource
    private SharedCardDAO sharedCardDAO;

    @Resource
    private SharedCardLuceneService sharedCardLuceneService;
    @Resource
    private StoreDAO storeDAO;

    public CardInterfaceImpl() {

    }

    public static final double RATE_MILE_TO_KM = 1.609344; // 英里和公里的比率
    public static final String LAT_FIELD = "lat";
    public static final String LON_FIELD = "lng";

    private SpatialContext ctx = SpatialContext.GEO;// "ctx" is the conventional
    // variable name
    int maxLevels = 11;
    SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);

    @Override
    public void update(int cardId, boolean update) {
        SharedCard card = sharedCardDAO.getSharedCardBycardId(cardId);
        if(card==null)
            logger.info("solr update cardId  is null");
        if (card != null) {
            logger.info("solr update cardId="+card.getCardid());
            if(card.getStoreid()!=null&&card.getStoreid()!=0) {
                Store store = storeDAO.getStoreBystoreId(card.getStoreid());
                if (store != null) {
                    card.setStoreaddress(store.getAddress());
                    card.setLongitude(store.getLongitude());
                    card.setLatitude(store.getLatitude());
                    card.setStorename(store.getName());
                    if(!StringUtils.isNullOrEmpty(store.getAd_code())){
                        card.setAd_code(store.getAd_code());
                        card.setAd_code_province(store.getAd_code().substring(0,2)+"0000");
                        card.setAd_code_city(store.getAd_code().substring(0,4) + "00");
                        card.setAd_code_region(store.getAd_code());
                    }
                }
            }
            else{
                card.setStoreaddress(card.getStoreaddress());
                card.setStorename(card.getStorename());
            }
            try {
                if (update) {
                    sharedCardLuceneService.updateSharedCard(card);
                } else {
                    sharedCardLuceneService.saveSharedCard(card);
                }
            } catch (Exception e) {
                logger.error("更新CardLucene失败", e);
            }
        }


    }

    @Override
    public void close() throws IOException {

    }

}
